import { Injectable } from '@nestjs/common';
import { CreateRediDto } from './dto/create-redi.dto';
import { UpdateRediDto } from './dto/update-redi.dto';
import { createClient, RedisClientType } from 'redis';

@Injectable()
export class RedisService {
  public redisClient: RedisClientType;
  constructor() {
    this.redisClient = createClient({ url: 'redis://localhost:6379' });
    this.redisClient.connect();
  }
  async getRedis(key: string) {
    const data = await this.redisClient.get(key);
    return data;
  }

  async setRedis(key: string, value: any) {
    const res = await this.redisClient.set(key, value);
    return res;
  }

  create(createRediDto: CreateRediDto) {
    return 'This action adds a new redi';
  }

  async findAll() {
    this.redisClient.publish('test', 'redis message');
    await this.setRedis('b', 1234);
    console.log(await this.getRedis('b'));
    // 队列相关
    let arr = [];
    arr.push(5);
    arr.push(6);
    arr.push(7);
    arr.push(8);
    arr.push(9);
    arr.unshift(4);
    arr.unshift(3);
    arr.unshift(2);
    arr.unshift(1);
    console.log(arr);
    arr.pop();
    console.log(arr);
    arr.shift();
    console.log(arr);
    return `This action returns all redis`;
  }

  findOne(id: number) {
    return `This action returns a #${id} redis`;
  }

  update(id: number, updateRediDto: UpdateRediDto) {
    return `This action updates a #${id} redis`;
  }

  remove(id: number) {
    return `This action removes a #${id} redis`;
  }
}
